<template>
    <div>
        <!-- 共用头部组件 -->
        <common-header
            headerTitle="我的联系方式"
            :hasBack="true"
            :hasHome="true"
        ></common-header>
        <!-- 共用头部组件 -->

        <div
            class="my-info-box"
            v-if="codeImg"
        >
            <img
                class="my-ewm"
                :src="codeImg"
            />
        </div>

        <div
            class="add-btn"
            @click="uploadCode"
        >上传联系方式二维码</div>
    </div>
</template>

<script>
import CommonHeader from '@/components/Header'
import wx from 'weixin-js-sdk'
export default {
    name: 'AgentInfo',
    components: {
        CommonHeader
    },
    data() {
        return {
            scrollStyle: {
                top: '1.11rem',
                left: '0',
                right: '0',
                bottom: '0'
            },
            codeImg: ''
        }
    },
    mounted() {
        this.getUserInfo()
    },
    methods: {
        getUserInfo() {
            this.codeImg = JSON.parse(
                sessionStorage.getItem('userInfo')
            ).link_qcode
        },
        uploadCode() {
            const that = this
            let ios
            let ua = navigator.userAgent.toLowerCase()
            if (ua.match(/iphone/i) == 'micromessenger') {
                ios = true
            } else {
                ios = false
            }
            wx.chooseImage({
                count: 1, // 默认9
                sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                success: function(res) {
                    let localIds = res.localIds
                    wx.uploadImage({
                        localId: localIds[0],
                        isShowProgressTips: 1,
                        success: function(res) {
                            let serverId = res.serverId
                            let postData = that.qs.stringify({
                                link_qcode: serverId
                            })
                            that.axios
                                .post('/api/user/info/update.json', postData)
                                .then(res => {
                                    const data = res.data
                                    if (data.status == 1) {
                                        that.commonDialog.alert(
                                            '提示',
                                            '上传成功！'
                                        )
                                        this.getUserInfo()
                                    } else {
                                        that.commonDialog.alert(
                                            '提示',
                                            data.msg
                                        )
                                    }
                                })
                            if (ios || window.__wxjs_is_wkwebview) {
                                let localId = localIds[0]
                                wx.getLocalImgData({
                                    localId: localId,
                                    success: function(res) {
                                        let localData = res.localData // localData是图片的base64数据，可以用img标签显示
                                        localData = localData.replace(
                                            'jgp',
                                            'jpeg'
                                        )
                                        that.codeImg = localData
                                    },
                                    fail: function(res) {
                                        that.commonDialog.alert(
                                            '提示',
                                            '该图片暂时无法查看'
                                        )
                                    }
                                })
                            } else {
                                that.codeImg = localIds[0]
                            }
                        }
                    })
                }
            })
        }
    }
}
</script>

<style lang="stylus" scoped>
.my-info-box
    padding-top 1rem
    text-align center
    .my-ewm
        width 8rem
.add-btn
    position absolute
    bottom 0
    left 0
    right 0
    height 1.29rem
    line-height 1.29rem
    text-align center
    background #d7463c
    font-size 0.38rem
    color #fff
</style>

